#include <vector>
#include <unordered_map>
#include <iostream>

#if 1 
class Solution
{
public:
    std::vector<int> twoSum(std::vector<int> &nums, int target)
    {
        std::unordered_map<int, int> hashtable;
        for (int i = 0; i < nums.size(); ++i)
        {
            auto it = hashtable.find(target - nums[i]);
            if (it != hashtable.end())
            {
                return {it->second, i};
            }
            hashtable[nums[i]] = i;
        }
        return {};
    }
};

#endif

#if 0
class Solution
{
public:
    std::vector<int> twoSum(std::vector<int> &nums, int target)
    {
        for (auto it = nums.begin(); it != nums.end(); it++)
        {
            int temp = target - *it;
            for (auto it_next = it + 1; it_next != nums.end(); it_next++)
            {
                if (*it_next == temp)
                {
                    // return {static_cast<int>(std::distance(nums.begin(), it)),
                    // static_cast<int>(std::distance(nums.begin(), it_next))}; 两种选择都可以，但是第二种写法更直观。
                    return {static_cast<int>((it - nums.begin())), static_cast<int>((it_next - nums.begin()))};
                }
            }
        }
        return {};
    }
};

#endif


int main()
{
    std::vector<int> nums = {2, 7, 11, 15};
    int target = 9;

    Solution sol;
    std::vector<int> result = sol.twoSum(nums, target);

    std::cout << "Indexes: " << result[0] << ", " << result[1] << std::endl;

    return 0;
}
